package com.cory.boot.component.easypoi;

import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import com.cory.boot.sys.entity.SysDictValue;
import com.cory.boot.sys.service.SysDictService;
import com.cory.boot.utils.CommonData;
import com.cory.utils.lang.StringUtils;
import com.google.common.collect.Lists;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

/**
 * 支持字典参数设置
 * 举例： @Excel(name = "性别", width = 15, dicCode = "sex")
 * 1、导出的时候会根据字典配置，把值1,2翻译成：男、女;
 * 2、导入的时候，会把男、女翻译成1,2存进数据库;
 *
 * @author Administrator
 * @version 1.0
 * @date 2022/2/6 12:43
 */
@Component
public class IExcelDictHandlerImpl implements IExcelDictHandler {

    @Autowired
    private SysDictService sysDictService;
    private static SysDictService dictService;


    @PostConstruct
    public void init() {
        dictService = this.sysDictService;
    }

    /**
     * 从值翻译到名称
     *
     * @param dictCode  字典Code
     * @param obj   对象
     * @param name  属性名称
     * @param value 属性值
     * @return
     */
    @Override
    public String toName(String dictCode, Object obj, String name, Object value) {
        if(value == null){
            return "";
        }
        String dictKey = String.valueOf(value);
        String dictName = "";
        //判断是否是多选的字典，比如客户标签，以“,"隔开保存的数据
        if(dictKey.contains(",")) {
            dictName = CommonData.getDictValues(dictCode,dictKey);
        }else{
            dictName = CommonData.getDictValue(dictCode,dictKey);
        }
        if(StringUtils.isEmpty(dictName)) {
            dictName = dictKey;
        }
        return dictName;
    }

    /**
     * 从名称翻译到值
     *
     * @param dictCode  字典Code
     * @param obj   对象
     * @param name  属性名称
     * @param value 属性值
     * @return
     */
    @Override
    public String toValue(String dictCode, Object obj, String name, Object value) {
        if(value == null){
            return "";
        }
        String str_value = String.valueOf(value);
        String dictKey = "";
        List<String> dictValuesList = null;
        //判断是否是多选的字典，比如客户标签，以“,"隔开保存的数据
        if(str_value.contains("|")) {
            dictValuesList = Arrays.asList(str_value.split("\\|"));
        }else{
            dictValuesList = Lists.newArrayList();
            dictValuesList.add(str_value);
        }

        for(String dictValue : dictValuesList) {
            String key = dictService.getDictKey(dictCode,dictValue);
            if(StringUtils.isEmpty(key)) {
                //自动创建，key为中文对应的拼音全称
                HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
                //拼音小写
                format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
                //不带声调
                format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
                try {
                    //转换成英文
                    key = PinyinHelper.toHanYuPinyinString(dictValue, format, "", true);
                    key = saveDict(dictCode,key,dictValue);
                } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                    badHanyuPinyinOutputFormatCombination.printStackTrace();
                }
            }
            if(StringUtils.isNotEmpty(dictKey)) {
                dictKey = dictKey + ",";
            }
            dictKey = dictKey + key;
        }

        return dictKey;
    }

    /**
     * 保存字典
     * @param dictKey
     * @param dictValue
     * @return
     */
    private String saveDict(String dictCode,String dictKey,String dictValue) {
        //验证字典是否已经存在
        SysDictValue sysDictValue = dictService.getDictValue(dictCode,dictKey);
        if(sysDictValue != null) {
            dictKey = dictKey + "1";
        }
        sysDictValue = new SysDictValue();
        sysDictValue.setDictCode(dictCode);
        sysDictValue.setDictKey(dictKey);
        sysDictValue.setDictValue(dictValue);
        dictService.saveDictValue(sysDictValue);
        return dictKey;
    }

}
